package com.oilsoft.shared.components
{
	import com.oilsoft.shared.vos.contour.Figure;
	import com.oilsoft.shared.vos.contour.Point;
	import com.oilsoft.shared.vos.contour.Polygon;
	import com.oilsoft.shared.vos.contour.Polyline;
	
	import flash.display.Graphics;
	import flash.display.GraphicsPathCommand;
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.ToolTip;
	import mx.core.UIComponent;
	import mx.events.FlexMouseEvent;
	import mx.managers.ToolTipManager;
	
	import spark.core.SpriteVisualElement;
	import spark.effects.Fade;

	//[Event(name="mouseOverFigure", type="flash.events.MouseEvent")]

	[Bindable]
	public class GraphicPolygon extends SpriteVisualElement
	{
		private var _command:Vector.<int>=new Vector.<int>();
		private var _data:Vector.<Number>=new Vector.<Number>();
		private var _figure:Figure=new Figure;
		private var  g:Graphics;
		private var tip:ToolTip;
		private var s:String;
		public function GraphicPolygon(figure:Figure)
		{

			super();
			setFigure(figure);
			this.addEventListener(MouseEvent.CLICK, onClickHandler);
			this.addEventListener(MouseEvent.MOUSE_OVER, onOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, onOverOutHandler);
			this.addEventListener(MouseEvent.MOUSE_MOVE, onOverHandler);
			
		}


		public function get figure():Figure
		{
			return _figure;
		}

		public function setFigure(figure:Figure):void
		{
			var objPoints:ArrayCollection=figure.points;
			var point:Point=new Point();
			_figure = figure;
			//var figures:ArrayCollection.<Figure>  = new ArrayCollection();
			var i:int = 10;
			for each (point in objPoints)
			{
				i++;
				if (point.index == 1)
				{
					_command.push(GraphicsPathCommand.MOVE_TO);
				}
				else
				{
					_command.push(GraphicsPathCommand.LINE_TO);
				}
				_data.push(point.x * 50.0, point.y * 50.0);
				
			}
			
			if (figure is Polygon)
			{
				var p:Point=objPoints.getItemAt(0) as Point;
				_command.push(GraphicsPathCommand.LINE_TO);
				_data.push(p.x * 50.0, p.y * 50.0);
			}
			if (figure is Polyline)
			{
			}
		}

		private function onClickHandler(e:MouseEvent):void
		{
			//g.clear();
			_command = new Vector.<int>();
			_data = new Vector.<Number>();
			var objPoints:ArrayCollection=figure.points;
			for each (var p:Point in objPoints) 
			{
				p.x+=2.0/50;
				p.y+=2.0/50;
			}
			figure.points=objPoints;
			setFigure(figure);
			drawComponent();
			
			
		}

		
		private function onOverHandler(e:MouseEvent):void
		{
			s="X: " + e.localX / 50.0 +"\n Y: " + e.localY/50.0 ;
			if(tip==null){
				tip = ToolTipManager.createToolTip(s,e.stageX + 10, e.stageY) as ToolTip;
			}else{
				tip.text=s;
			}
				
		}
		private function onOverOutHandler(e:MouseEvent):void
		{
			if(tip!=null){
			ToolTipManager.destroyToolTip(tip);
			tip=null;
			}
		}

		public function drawComponent():void
		{
			g=this.graphics;
			g.clear();
		    //g.beginFill(0x000000);
			g.lineStyle(2, 0x000000);
			g.drawPath(_command, _data);
			g.endFill();
		}

		/**
		 * Called during the validation pass by the Flex LayoutManager via the layout object.
		 * Sets the layout size of the element. This is the size that the element uses to draw on screen.
		 * @param width
		 * @param height
		 * @param postLayoutTransform
		 *
		 */
		override public function setLayoutBoundsSize(width:Number, height:Number, postLayoutTransform:Boolean=true):void
		{
			super.setLayoutBoundsSize(width, height, postLayoutTransform);

			drawComponent();
		}
		
	}
}